package com.gw.business.system.menu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.gw.business.system.menu.model.TreeModel;
import com.gw.business.system.menu.service.ISystemService;
import com.gw.platform.service.impl.DefaultService;

/**
 * 类功能:系统管理实现类
 * 
 * <p>创建者:关博</p>
 * 
 * <p>创建时间:2014-06-23</p>
 * 
 * <p>修改者:</p>
 * 
 * <p>修改时间:</p>
 * 
 * <p>修改原因:</p>
 * 
 * <p>审核者:</p>
 * 
 * <p>审核时间:</p>
 * 
 * <p>审核意见:</
 * p>
 */
@Service
public class SystemServiceImpl extends DefaultService implements ISystemService{
	
	/**
	 * 生成菜单树
	 */
	public List<TreeModel> getMeunTree(String treeSQL, Object param) {
		List<TreeModel> item = (List<TreeModel>) find(treeSQL, param);
        if (item == null)
            return item;
        
        //构造父子集合
        for (TreeModel current : item) {
        	for (TreeModel parent : item) {
                if(parent.getId() == current.getParentId()){
                	if(parent.getItem()==null){
                		parent.setItem(new ArrayList<TreeModel>());
                	}
                	parent.getItem().add(current);
                	current.setTop("0");//一级菜单标识
                }
            }
        }
        
        //去除Top=null的菜单  因为Top=null是二级菜单 这些已经添加到一级菜单下 
        //去除集合中多余项
        for (int i=0;i<item.size();) {
        	TreeModel current=item.get(i);
        	if(current.getTop()==null){
        		i++;
        	}else{
        		item.remove(i);
        	}
        }
        return item;
	}
}
